<html>
<head><meta charset="utf-8"><title>Optimizing Box::new · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html">Optimizing Box::new</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217925584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217925584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erick Tryzelaar <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217925584">(Nov 25 2020 at 19:02)</a>:</h4>
<p>Hello everyone! I was thinking about playing around with the compiler over vacation, and, well, it's changed a bit since I last saw it 5 years ago (wow it's been a while).</p>
<p>I was thinking about trying to optimize box allocations to avoid blowing the stack when boxing a large array or a deeply nested async function. I noticed that <code>box [5; 4096*4096]</code> will do the right thing and initialize the array with the heap pointer, rather than on the stack and doing a copy, but <code>let x = [5; 4096 * 4096]; box x</code> (and <code>Box::new([5; 4096*4096])</code> if we turn on mir-opt-level=2) allocate on the stack and copy: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=release&amp;edition=2018&amp;gist=6c9df24d2f6279d9a67d2957da16c5c1">https://play.rust-lang.org/?version=nightly&amp;mode=release&amp;edition=2018&amp;gist=6c9df24d2f6279d9a67d2957da16c5c1</a></p>
<p>It seems like the <code>dest_prop</code> optimization should be able to handle this, but it explicitly excludes derefs in the destination since it can't prove a generic pointer isn't pointing at arbitrary memory. My idea though is we could relax this for locally created <code>_X = Box(foo)</code> statements, since we should be able to prove that the pointer is valid, at least for the first write (and no one tries to read from the pointer before we write to it). Does this sound like a reasonable assumption, or are there still soundness issues with doing this?</p>
<p>Second question, since an optimization like this would help us avoid blowing the stack in a number of places, would it be worthwhile doing this as a cheap separate pass  that could always run (if that's even possible), rather than trying to do this in dest_prop?</p>



<a name="217929675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217929675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217929675">(Nov 25 2020 at 19:38)</a>:</h4>
<p>hmm, this code produces MIR that even intra-basic-block forwards copy propagation would handle:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">run</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">([</span><span class="mi">9</span><span class="p">;</span><span class="w"> </span><span class="mi">100</span><span class="p">]);</span><span class="w"></span>

<span class="w">    </span><span class="n">print</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">print</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u64</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"{:x?}"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="n">bb0</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_1</span><span class="p">);</span><span class="w">                 </span><span class="c1">// scope 0 at ./example.rs:2:9: 2:10</span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_2</span><span class="p">);</span><span class="w">                 </span><span class="c1">// scope 0 at ./example.rs:2:22: 2:30</span>
<span class="w">        </span><span class="n">_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="k">const</span><span class="w"> </span><span class="mi">9_</span><span class="k">u64</span><span class="p">;</span><span class="w"> </span><span class="mi">100</span><span class="p">];</span><span class="w">         </span><span class="c1">// scope 0 at ./example.rs:2:22: 2:30</span>
<span class="w">        </span><span class="n">_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span><span class="p">([</span><span class="kt">u64</span><span class="p">;</span><span class="w"> </span><span class="mi">100</span><span class="p">]);</span><span class="w">            </span><span class="c1">// scope 2 at ./example.rs:2:13: 2:31</span>
<span class="w">        </span><span class="p">(</span><span class="o">*</span><span class="n">_1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w">                 </span><span class="c1">// scope 2 at ./example.rs:2:13: 2:31</span>
<span class="w">        </span><span class="n">StorageDead</span><span class="p">(</span><span class="n">_2</span><span class="p">);</span><span class="w">                 </span><span class="c1">// scope 0 at ./example.rs:2:30: 2:31</span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_3</span><span class="p">);</span><span class="w">                 </span><span class="c1">// scope 1 at ./example.rs:4:5: 4:15</span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_4</span><span class="p">);</span><span class="w">                 </span><span class="c1">// scope 1 at ./example.rs:4:11: 4:14</span>
<span class="w">        </span><span class="n">_5</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">(</span><span class="o">*</span><span class="n">_1</span><span class="p">);</span><span class="w">                     </span><span class="c1">// scope 1 at ./example.rs:4:11: 4:14</span>
<span class="w">        </span><span class="n">_4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_5</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="kt">u64</span><span class="p">]</span><span class="w"> </span><span class="p">(</span><span class="n">Pointer</span><span class="p">(</span><span class="n">Unsize</span><span class="p">));</span><span class="w"> </span><span class="c1">// scope 1 at ./example.rs:4:11: 4:14</span>
<span class="w">        </span><span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="n">_4</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="k">return</span>: <span class="nc">bb1</span><span class="p">,</span><span class="w"> </span><span class="n">unwind</span>: <span class="nc">bb3</span><span class="p">];</span><span class="w"> </span><span class="c1">// scope 1 at ./example.rs:4:5: 4:15</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="217929754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217929754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217929754">(Nov 25 2020 at 19:39)</a>:</h4>
<p>maybe try running that with the pass from <a href="https://github.com/rust-lang/rust/pull/76723">https://github.com/rust-lang/rust/pull/76723</a>?</p>



<a name="217929802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217929802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217929802">(Nov 25 2020 at 19:39)</a>:</h4>
<p>hmm, actually, why doesn't this get const-propagated across the <code>_1 = Box</code>?</p>



<a name="217929870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217929870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217929870">(Nov 25 2020 at 19:40)</a>:</h4>
<p>(code I'm testing with is at <a href="https://rust.godbolt.org/z/YjKTnn">https://rust.godbolt.org/z/YjKTnn</a>)</p>



<a name="217937515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217937515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erick Tryzelaar <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217937515">(Nov 25 2020 at 20:49)</a>:</h4>
<p>I'm on my phone now, so I'll check later, but I think it has to do with the array type. If you do "let x = 5; box x" I think that does do const propagation</p>



<a name="217951292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951292">(Nov 25 2020 at 23:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20Box.3A.3Anew/near/217929802">said</a>:</p>
<blockquote>
<p>hmm, actually, why doesn't this get const-propagated across the <code>_1 = Box</code>?</p>
</blockquote>
<p>_1 and _2 could be aliases. Do we have alias analysis on MIR yet?</p>



<a name="217951360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951360">(Nov 25 2020 at 23:34)</a>:</h4>
<p>Lack of alias analysis is a long standing limiter for various propagation passes. If we had one, we'd have propagations since… /me checks.</p>



<a name="217951384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951384">(Nov 25 2020 at 23:35)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/33628">2016</a>.</p>



<a name="217951470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951470">(Nov 25 2020 at 23:36)</a>:</h4>
<p><code>_1</code> is freshly allocated, it can't alias anything</p>



<a name="217951622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951622">(Nov 25 2020 at 23:38)</a>:</h4>
<p>Right, but you don't have a generic way to know that for an arbitrary MIR.</p>



<a name="217951655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951655">(Nov 25 2020 at 23:39)</a>:</h4>
<p>You could hack in an approximation of this into your optimisation, I guess, but it would have to be super conservative.</p>



<a name="217951714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951714">(Nov 25 2020 at 23:40)</a>:</h4>
<p>But even if there's a pointer to <code>_2</code>, the array could still be propagated into the right-hand side of the assignment</p>



<a name="217951733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951733">(Nov 25 2020 at 23:40)</a>:</h4>
<p>In this particular case, just checking if <code>_2</code> ever has its address taken would be enough to rule out aliasing</p>



<a name="217951739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951739">(Nov 25 2020 at 23:40)</a>:</h4>
<p>I'm not arguing that you could not, in this particular example.</p>



<a name="217951790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951790">(Nov 25 2020 at 23:41)</a>:</h4>
<p>Let me rephrase myself: if you're thinking of adding some sort of a check to propagation to handle this specific case that would match this exact pattern, that's really a peephole optimisation more suited for something like instcombine</p>



<a name="217951878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951878">(Nov 25 2020 at 23:43)</a>:</h4>
<p>pattern match <code>x = const _; y = Box(_); (*y) = x</code> and replace with <code>y = Box(_); (*y) = const _</code></p>



<a name="217951993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217951993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217951993">(Nov 25 2020 at 23:45)</a>:</h4>
<p>I strongly suspect that in the current propagation passes they are required to be overly pessimistic in cases like these because there's some MIR generated by some code somewhere that caused this exact pattern to miscompile, forcing implementer to just bail out for now.</p>



<a name="217952574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217952574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217952574">(Nov 25 2020 at 23:55)</a>:</h4>
<p>I'm not suggesting to add such a special case, I was expecting this to be already handled by the pass, given what I've seen it propagate in practice</p>



<a name="217952606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217952606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217952606">(Nov 25 2020 at 23:56)</a>:</h4>
<p>But I didn't know that we're this close to fixing this long-standing issue with <code>Box</code>, that's pretty cool</p>



<a name="217954438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217954438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217954438">(Nov 26 2020 at 00:27)</a>:</h4>
<p>I suppose a pass that would be more generally useful for <code>Box::new</code> would be one that hoists the <code>Box</code> allocation backwards to then enable dest prop to eliminate the copy. This would ignore side-effects and function calls, which allows non-trivial initializers.</p>



<a name="217954656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217954656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217954656">(Nov 26 2020 at 00:31)</a>:</h4>
<p>Hmm, but dest prop doesn't handle pointers at all, so that would have to be smarter too</p>



<a name="217963878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217963878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erick Tryzelaar <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217963878">(Nov 26 2020 at 04:06)</a>:</h4>
<p>Ooh something like instcombine could work for a short term fix until something more general comes along</p>



<a name="217974189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/217974189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#217974189">(Nov 26 2020 at 08:10)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="212698">@Félix Fischer</span> ^ const prop with dataflow use case</p>



<a name="221570460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/221570460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erick Tryzelaar <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#221570460">(Jan 04 2021 at 19:41)</a>:</h4>
<p>Got a little time to think about this again. Do we have any optimizations that target specific functions, like llvm intrinsics? I was thinking about cheating a little bit in order to do something like:</p>
<ul>
<li>Annotate <code>Box::new</code> with a lang item, or some other mechanism to identify this function.</li>
<li>In the HIR-&gt;MIR pass (or someplace more appropriate), identify calls to <code>Box::new($expr)</code> and directly generate <code>box $expr</code> instead of <code>_1 = $expr; Box::new(move $1)</code></li>
</ul>
<p>That'd get us an easy win for direct calls to <code>Box::new([0; 1024])</code>, and might allow const prop, or some other pass, to optimize <code>let x = [0; 1024]; Box::new(x)</code>.</p>



<a name="221571845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20Box%3A%3Anew/near/221571845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20Box.3A.3Anew.html#221571845">(Jan 04 2021 at 19:51)</a>:</h4>
<p>A few targeting intrinsics. MIR building special cases <code>move_val_init</code> to evaluate argument directly into destination without introducing a temporary copy (although there is PR opened that removes it), and intrinsics lowering pass.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>